package com.sfm.tour;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;

public class SfmtourActivity extends Activity {
    
	public static ArrayList<Movie> movieData;
	public static Movie selectedMovie;
	public static RelativeLayout tourLayout;
	public static boolean useMovie = false;
	public static boolean useRadius = false;
	public static double radiusMiles;
	Spinner spinnerMovie, spinnerRadius;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tourLayout = (RelativeLayout)findViewById(R.id.tourlayout);
		tourLayout.setVisibility(View.GONE);
        XMLFunctions myXml= new XMLFunctions();
        movieData = myXml.getMovies(this);
        ArrayList<String> movieTitles = getMovieTitles();
        
        //Select Movie
        spinnerMovie = (Spinner) findViewById(R.id.spinner_selectmovie);
        spinnerMovie.setOnItemSelectedListener(new MovieOnItemSelectedListener());        
        ArrayAdapter<String> adapterMovie = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, movieTitles);
        adapterMovie.insert("Select Movie",0);
        adapterMovie.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerMovie.setAdapter(adapterMovie);
        
        //Select Location Radius
        spinnerRadius = (Spinner) findViewById(R.id.spinner_radius);
        spinnerRadius.setOnItemSelectedListener(new RadiusOnItemSelectedListener());
        ArrayAdapter<CharSequence> adapterRadius = ArrayAdapter.createFromResource(
                this, R.array.select_locationradius, android.R.layout.simple_spinner_item);
        adapterRadius.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerRadius.setAdapter(adapterRadius);
        
        Button go = (Button) findViewById(R.id.button_go);
       
    }
    
 
    public void launchTag(View v) {
    	Intent mapIntent = new Intent(getApplicationContext(), SFMapTagActivity.class);
        startActivity(mapIntent);
        
    }
    
	public void onClick(View v) {
		
        if(!useRadius && !useMovie) {
        	Toast.makeText(getApplicationContext(),"Select either Movie or Location radius",Toast.LENGTH_SHORT).show();
        	
        } else if(useRadius && useMovie) {
        	Toast.makeText(getApplicationContext(),"You have selected both. Showing all movies around current location",Toast.LENGTH_SHORT).show();
        	useMovie = false;
        	Intent mapIntent = new Intent(getApplicationContext(), SFMapActivity.class);
            startActivity(mapIntent);
            
        } else {
        	Intent mapIntent = new Intent(getApplicationContext(), SFMapActivity.class);
            startActivity(mapIntent);
            
        }
		
    }

	
	public void showTour(View v) {
		tourLayout.setVisibility(View.VISIBLE);
	}
    
    public ArrayList<String> getMovieTitles() {
    	ArrayList<String> titles = new ArrayList<String> ();
    	for(Movie m : movieData) {
    		titles.add(m.getTitle());
    	}
    	
    	return titles;
    	
    }

    
    public class MovieOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
          
          String selectedTitle =  parent.getItemAtPosition(pos).toString();
            for(Movie m : movieData) {
	        	  if(m.getTitle() == selectedTitle) {
	        		  selectedMovie = new Movie(m);
	        		  useMovie = true;
	        	  }
	          }      
        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    }
    
    public class RadiusOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
          
          String selectedRadius =  parent.getItemAtPosition(pos).toString();
          useRadius = true;
          switch(pos) {
          case 0:	radiusMiles = 0; 
     	 			useRadius = false;
     	 break;
          case 1: 	radiusMiles = 0.5;
          break;
          case 2: radiusMiles = 1;
          break;
          case 3: radiusMiles = 5;
          break;
          case 4: radiusMiles = 10;
          break;
          default: radiusMiles = 1;
          break;
          }
          
        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    }
    

}